//
//  WYJWelcomeViewController.swift
//  MySinaWeibo
//
//  Created by eastriver on 16/10/11.
//  Copyright © 2016年 believewilliam. All rights reserved.
//

import UIKit
import SDWebImage
let bottomMargin: CGFloat = 120
class WYJWelcomeViewController: UIViewController {
    // 属性
    lazy var iconImgV: UIImageView = {
        let imgV = UIImageView(image:#imageLiteral(resourceName: "avatar_default_big"))
   
        imgV.layer.cornerRadius = 42
        imgV.layer.masksToBounds = true
        imgV.layer.borderWidth = 1
        imgV.layer.borderColor = UIColor.orange.cgColor
        
        
        return imgV
    }()
    
    lazy var welcomeLab: UILabel = {
        let lab = UILabel(title: "欢迎归来", textColor: #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1), fontSize: 16)
        lab.alpha = 0
        return lab
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        setupUI()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    func setupUI() {
        self.view.addSubview(iconImgV)
        view.addSubview(welcomeLab)
        // 设置frame
        iconImgV.snp.makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-bottomMargin)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        welcomeLab.snp.makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(iconImgV.snp.bottom).offset(20)
        }
        iconImgV.sd_setImage(with: WYJUserAccountViewModel.sharedAccountViewModel.iconURL)
    }
    
    func startAnimation() {
        let offset = -(UIScreen.main.bounds.height - bottomMargin - 85)
        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: {
            self.iconImgV.snp.updateConstraints({ (make) in
                make.bottom.equalTo(self.view).offset(offset)
            })
            // 刷新view
            self.view.layoutIfNeeded()
        }) { (_) in
            UIView.animate(withDuration: 0.5, animations: {
                self.welcomeLab.alpha = 1
                
                }, completion: { (_) in
                    // 发出通知，切换应用程序的根控制器
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: kNotificationName), object: "welcome")
            })
        }
    }
    
    deinit {
        print("欢迎控制器被销毁了")
    }
    
}
